package com.huadao.cart_ccs.modle;

import com.huadao.cart_ccs.base.BaseSerialPortModel;
import com.huadao.cart_ccs.common.ByteUtils;
import com.huadao.cart_ccs.data.bo.CommandWord;
import com.huadao.cart_ccs.data.bo.DeviceBO;
import com.huadao.cart_ccs.data.bo.Protocol;
import com.huadao.cart_ccs.exception.BytesLengthException;

public class DeviceModle extends BaseSerialPortModel {

    public void queryMAC_AgentNumber(){
        Protocol params= ByteUtils.generateProtocol(CommandWord.read,new byte[]{DeviceBO.ModularType_DeviceMAC,DeviceBO.deviceMAC_NO1,DeviceBO.read},null);
        serialPortRequest(params);
    }

    /**
     *
     * @param ip 4字节IP
     * @param agentNumber 2字节代理商编号
     */
    public void modifyIp_AgentNumber(byte[] ip,byte[] netmask,byte[] agentNumber) throws BytesLengthException {
        if(ip==null){
            throw new BytesLengthException("ip is null");
        }else if(ip.length!=4){
            throw new BytesLengthException("ip length is illegal: "+ip.length+" should be 4");
        }
        if(netmask==null){
            throw new BytesLengthException("netMask is null");
        }else if(netmask.length!=4){
            throw new BytesLengthException("netMask length is illegal: "+ip.length+" should be 4");
        }
        if(agentNumber==null){
            throw new BytesLengthException("agentNumber is null");
        }else if(agentNumber.length!=2){
            throw new BytesLengthException("agentNumber length is illegal: "+agentNumber.length+" should be 2");
        }
        byte[] orderContent=ByteUtils.mergeByteArray(ip,netmask,agentNumber);
        Protocol params= ByteUtils.generateProtocol(CommandWord.write,new byte[]{DeviceBO.ModularType_DeviceMAC,DeviceBO.deviceMAC_NO1,DeviceBO.modify},orderContent);
        serialPortRequest(params);
    }

    public void readTime(){
        Protocol params= ByteUtils.generateProtocol(CommandWord.read,new byte[]{DeviceBO.ModularType_DeviceMAC,DeviceBO.deviceMAC_NO2,DeviceBO.read},null);
        serialPortRequest(params);
    }

    public void modifyTime(byte year,byte month,byte day,byte day_week,byte hour,byte minute,byte second,byte am_pm){
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("修改时间: ");
        stringBuilder.append(ByteUtils.byte2int(year));
        stringBuilder.append("年");
        stringBuilder.append(ByteUtils.byte2int(month));
        stringBuilder.append("月");
        stringBuilder.append(ByteUtils.byte2int(day));
        stringBuilder.append("日");
        stringBuilder.append(ByteUtils.byte2int(hour));
        stringBuilder.append("时");
        stringBuilder.append(ByteUtils.byte2int(minute));
        stringBuilder.append("分");
        stringBuilder.append(ByteUtils.byte2int(second));
        stringBuilder.append("秒");
        stringBuilder.append(" ");
        if(am_pm==0){
            stringBuilder.append("上午");
        }else if(am_pm==1){
            stringBuilder.append("下午");
        }

        byte[] orderContent=new byte[8];
        orderContent[0]=year;
        orderContent[1]=month;
        orderContent[2]=day;
        orderContent[3]=day_week;
        orderContent[4]=hour;
        orderContent[5]=minute;
        orderContent[6]=second;
        orderContent[7]=am_pm;
//        byte[] params= ByteUtils.generateParams(CommandWord.write,new byte[]{DeviceBO.ModularType_DeviceMAC,DeviceBO.deviceMAC_NO2,DeviceBO.modify},orderContent);
        Protocol params= ByteUtils.generateProtocol(CommandWord.write,new byte[]{DeviceBO.ModularType_DeviceMAC,DeviceBO.deviceMAC_NO2,DeviceBO.modify},orderContent);
        serialPortRequest(params);
    }

    public void shutDown(){
        Protocol params= ByteUtils.generateProtocol(CommandWord.write,new byte[]{DeviceBO.ModularType_DeviceMAC,DeviceBO.deviceMAC_SHUTDOWN,DeviceBO.shutdown},null);
        serialPortRequest(params);
    }

    public void sendData(byte[] para) {
        serialPortRequest(para);
    }
}
